{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "4387798b",
   "metadata": {},
   "source": [
    "# PCA降维，聚类可视化\n",
    "\n",
    "支持2D、3D降维可视化，数据必须为numpy。并且最后一列为聚类类别。\n",
    "\n",
    "1. 2D npy文件生成，使用OKT-gen_feature_cluster。\n",
    "2. 3D npy文件生成，使用OKT-gen_habitat_cluster。\n",
    "\n",
    "使用过程中，修改path2npy路径即可。\n",
    "\n",
    "**注意**： 这个可视化底层调用的接口某些资源墙内加载可能比较慢，如果没有出现报错，请多等待，必要时候请科学上网。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ca3fe227",
   "metadata": {},
   "outputs": [],
   "source": [
    "from onekey_algo.custom.Manager import onekey_show\n",
    "\n",
    "onekey_show('模型可解释性-SHAP')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "84d7c11f",
   "metadata": {},
   "source": [
    "# t-SNE降维可视化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8a4d06b9",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "from onekey_algo.custom.components.comp1 import analysis_features\n",
    "\n",
    "feature_file = r'C:\\Users\\onekey\\Desktop\\Sol23. 无监督聚类 + 弱监督病理\\features\\cf/compress_features.csv'\n",
    "label_file = r'C:\\Users\\onekey\\Desktop\\Sol23. 无监督聚类 + 弱监督病理\\features\\cluster\\cluster_8/clusters_compress_features.csv'\n",
    "\n",
    "feature_data = pd.read_csv(feature_file)\n",
    "label_data = pd.read_csv(label_file)\n",
    "if 'label' not in label_data.columns:\n",
    "    label_data['label'] = label_data['ClusterID']\n",
    "unique_label = sorted(list(np.unique(label_data['label'])))\n",
    "data = pd.merge(feature_data, label_data, on='ID', how='inner')\n",
    "plt.figure(figsize=(15, 10))\n",
    "analysis_features(data[[c for c in data.columns if c != 'label']], data['label'], save_dir='img', \n",
    "                  legend=[f'Cluster-{c}' for c in unique_label])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c6b4fc94",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
